﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Common;
using Blue.ResourceManager;
using EngineOfEvermore.Imaging;
using System.ComponentModel;

namespace EngineOfEvermore.Testing
{
    public class RotationTester
    {
        [SerializerIgnore]
        [Browsable( false )]
        class RotationUpdaterComponent : SceneObjectComponent
        {
            class InternalUpdateTask : Task
            {
                RotationUpdaterComponent _owner;
                float _flipTimer = 1;
                bool _flipX = true;

                public InternalUpdateTask( RotationUpdaterComponent owner ) :
                    base( 0 )
                {
                    _owner = owner;
                }

                public override void Execute( EngineOfEvermore.Common.LogicUpdateEventArgs e )
                {
                    _owner._vcSibling.RotationOffset += e.ElapsedTime * 10;

                    _flipTimer -= e.ElapsedTime;

                    if ( _flipTimer < 0 )
                    {
                        var sprite = (AbstractSprite) _owner._vcSibling.Children.Single();

                        if ( _flipX )
                        {
                            sprite.FlipX = !sprite.FlipX;
                        }
                        else
                        {
                            sprite.FlipY = !sprite.FlipY;
                        }

                        _flipX = !_flipX;
                        _flipTimer += 1;
                    }
                }
            }

            VisualComponent _vcSibling;
            InternalUpdateTask _updateTask;

            public RotationUpdaterComponent()
            {
                _updateTask = new InternalUpdateTask( this );
            }

            public VisualComponent VisualComponent
            {
                get { return _vcSibling; }
                set { _vcSibling = value; }
            }

            protected override void OnSiblingAdded( SceneObjectComponent sibling )
            {
                if ( sibling is VisualComponent )
                {
                    this.VisualComponent = sibling as VisualComponent;
                }

                base.OnSiblingAdded( sibling );
            }

            protected override void OnSiblingRemoved( SceneObjectComponent sibling )
            {
                if ( sibling is VisualComponent )
                {
                    this.VisualComponent = null;
                }

                base.OnSiblingRemoved( sibling );
            }

            protected override void OnAddToScene( SceneGraph scene )
            {
                base.OnAddToScene( scene );

                scene.TimeContext.AddRepeatingTask( _updateTask );
            }

            protected override void OnRemoveFromScene( SceneGraph scene )
            {
                scene.TimeContext.RemoveRepeatingTask( _updateTask );

                base.OnRemoveFromScene( scene );
            }

            public override SceneObjectComponent Clone()
            {
                throw new NotImplementedException();
            }
        }


        public static SceneObject CreateStaticSpriteTestObject()
        {
            var rotationUpdater = new RotationUpdaterComponent();

            var visualComponent = new VisualComponent();

            var staticSprite = new StaticSprite();
            staticSprite.ImageMap = ResourceManager.GetResource<ImageMap>( 
                "/ImageMaps/somsheet.ximp" );
            staticSprite.Scale = new Vector2d( 2, 2 );

            visualComponent.Children.Add( staticSprite );

            var sceneObject = new SceneObject();

            sceneObject.AddComponent( rotationUpdater );
            sceneObject.AddComponent( visualComponent );

            return sceneObject;
        }

        public static SceneObject CreateAnimatedSpriteTestObject()
        {
            var rotationUpdater = new RotationUpdaterComponent();

            var visualComponent = new VisualComponent();

            var animatedSprite = new AnimatedSprite();
            animatedSprite.Animations["X"] = ResourceManager.GetResource<FrameAnimation>( 
                "Animations/SomBoyWalkRight.xani" );
            animatedSprite.Scale = new Vector2d( 2, 2 );
            animatedSprite.CurrentAnimation = "X";

            visualComponent.Children.Add( animatedSprite );

            var sceneObject = new SceneObject();

            sceneObject.AddComponent( rotationUpdater );
            sceneObject.AddComponent( visualComponent );

            return sceneObject;
        }
    }
}
